<template>
  <div class="login-container">
    <div class="background-shapes">
      <div class="shape shape-1"></div>
      <div class="shape shape-2"></div>
      <div class="shape shape-3"></div>
    </div>
    
    <div class="form-container">
      <template v-if="!isRegister">
        <h2>登录</h2>
        <form @submit.prevent="handleLogin">
          <div class="input-group">
            <i class="icon">📧</i>
            <input v-model="loginEmail" type="email" placeholder="邮箱" required />
          </div>
          <div class="input-group">
            <i class="icon">🔒</i>
            <input v-model="loginPassword" type="password" placeholder="密码" required />
          </div>
          <button type="submit">登录</button>
        </form>
        <p>还没有账户？ <a href="#" @click.prevent="toggleForm">注册</a></p>
      </template>

      <template v-else>
        <h2>注册</h2>
        <form @submit.prevent="handleRegister">
          <div class="input-group">
            <i class="icon">📧</i>
            <input v-model="registerEmail" type="email" placeholder="邮箱" required />
          </div>
          <div class="input-group">
            <i class="icon">🔒</i>
            <input v-model="registerPassword" type="password" placeholder="密码" required />
          </div>
          <button type="submit">注册</button>
        </form>
        <p>已有账户？ <a href="#" @click.prevent="toggleForm">登录</a></p>
      </template>
    </div>
  </div>
</template>

<script setup>
import { ref, defineEmits, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const emit = defineEmits(['login'])

const isRegister = ref(false)

const toggleForm = () => {
  isRegister.value = !isRegister.value
}

const loginEmail = ref('')
const loginPassword = ref('')
const registerEmail = ref('')
const registerPassword = ref('')

const usersKey = 'users'

const router = useRouter()

// 初始化默认用户
onMounted(() => {
  const users = localStorage.getItem(usersKey)
  if (!users) {
    const defaultUser = { email: '123456@qq.com', password: '123456'}
    localStorage.setItem(usersKey, JSON.stringify([defaultUser]))
  }
})

// 辅助函数：获取用户列表
function getUsers() {
  const users = localStorage.getItem(usersKey)
  return users ? JSON.parse(users) : []
}

// 辅助函数：保存用户列表
function saveUsers(users) {
  localStorage.setItem(usersKey, JSON.stringify(users))
}

// 处理登录
function handleLogin() {
  const users = getUsers()
  const user = users.find(
    user => user.email === loginEmail.value && user.password === loginPassword.value
  )
  if (user) {
    localStorage.setItem('currentUser', JSON.stringify(user))
    router.push('/user')
  } else {
    alert('无效的邮箱或密码')
  }
}

// 处理注册
function handleRegister() {
  const users = getUsers()
  const existingUser = users.find(user => user.email === registerEmail.value)
  if (existingUser) {
    alert('用户已存在')
    return
  }
  users.push({ email: registerEmail.value, password: registerPassword.value })
  saveUsers(users)
  alert('注册成功！请登录')
  // 清空注册表单
  registerEmail.value = ''
  registerPassword.value = ''
  // 切换回登录表单
  isRegister.value = false
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

.background-shapes {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.shape {
  position: absolute;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  animation: float 15s infinite;
}

.shape-1 {
  width: 300px;
  height: 300px;
  top: -150px;
  left: -150px;
  animation-delay: 0s;
}

.shape-2 {
  width: 200px;
  height: 200px;
  bottom: -100px;
  right: -100px;
  animation-delay: 5s;
}

.shape-3 {
  width: 150px;
  height: 150px;
  bottom: 50%;
  left: 50%;
  animation-delay: 10s;
}

@keyframes float {
  0% {
    transform: translate(0, 0) rotate(0deg);
  }
  33% {
    transform: translate(30px, 30px) rotate(120deg);
  }
  66% {
    transform: translate(-20px, 20px) rotate(240deg);
  }
  100% {
    transform: translate(0, 0) rotate(360deg);
  }
}

.form-container {
  position: relative;
  background: rgba(255, 255, 255, 0.95);
  padding: 50px 60px;
  border-radius: 20px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  width: 480px;
  backdrop-filter: blur(10px);
  transition: transform 0.3s ease;
}

.form-container:hover {
  transform: translateY(-5px);
}

.form-container h2 {
  text-align: center;
  margin-bottom: 35px;
  color: #333;
  font-size: 32px;
  font-weight: 600;
}

.input-group {
  position: relative;
  margin-bottom: 25px;
  margin-top: 25px;
  margin-right: 75px;
}

.icon {
  position: absolute;
  left: 20px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 20px;
  color: #666;
}

.form-container input {
  width: 100%;
  padding: 18px 18px 18px 55px;
  border: 1px solid rgba(108, 99, 255, 0.2);
  border-radius: 12px;
  outline: none;
  font-size: 17px;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.9);
}

.form-container input:focus {
  border-color: #6c63ff;
  box-shadow: 0 0 0 2px rgba(108, 99, 255, 0.1);
}

.form-container button {
  width: 100%;
  padding: 18px;
  background: linear-gradient(45deg, #6c63ff, #3071b3);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 15px;
}

.form-container button:hover {
  background: linear-gradient(45deg, #3071b3, #6c63ff);
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(108, 99, 255, 0.3);
}

.form-container p {
  text-align: center;
  margin-top: 25px;
  color: #666;
  font-size: 15px;
}

.form-container a {
  color: #6c63ff;
  text-decoration: none;
  font-weight: 600;
  font-size: 15px;
  transition: color 0.3s ease;
}

.form-container a:hover {
  color: #3071b3;
  text-decoration: underline;
}

@media (max-width: 576px) {
  .form-container {
    width: 95%;
    padding: 40px 30px;
  }
  
  .form-container h2 {
    font-size: 28px;
    margin-bottom: 30px;
  }
  
  .form-container input {
    font-size: 16px;
    padding: 15px 15px 15px 50px;
  }
  
  .form-container button {
    font-size: 16px;
    padding: 15px;
  }
  
  .icon {
    font-size: 18px;
    left: 18px;
  }
}
</style> 